<template>
  <div class="app-container">
    <div class="back-item">
      <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
        <el-form-item label="名称" prop="bannerName" v-if="showSearch">
          <el-input
              v-model="queryParams.bannerName"
              placeholder="请输入名称"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="轮播图" prop="bannerImg" v-if="showSearch">
          <el-input
              v-model="queryParams.bannerImg"
              placeholder="请输入轮播图"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="跳转链接" prop="linkUrl" v-if="showSearch">
          <el-input
              v-model="queryParams.linkUrl"
              placeholder="请输入跳转链接"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="排序" prop="sort" v-if="showSearch">
          <el-input
              v-model="queryParams.sort"
              placeholder="请输入排序"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="Search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button type="warning" icon="Refresh" size="mini" @click="resetQuery">重置</el-button>
          <el-button size="mini" @click="changSearch">{{showSearch?"常用条件":"所有条件"}}</el-button>
        </el-form-item>
      </el-form>
    </div>

    <div class="back-item">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
              type="primary"
              plain
              icon="Plus"
              size="mini"
              @click="handleAdd"
              v-hasPermi="['mall:banner:add']"
          >新增
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
              type="success"
              plain
              icon="Edit"
              size="mini"
              :disabled="single"
              @click="handleUpdate"
              v-hasPermi="['mall:banner:edit']"
          >修改
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
              type="danger"
              plain
              icon="Delete"
              size="mini"
              :disabled="multiple"
              @click="handleDelete"
              v-hasPermi="['mall:banner:remove']"
          >删除
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
              type="warning"
              plain
              icon="Download"
              size="mini"
              :loading="exportLoading"
              @click="handleExport"
              v-hasPermi="['mall:banner:export']"
          >导出
          </el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
      </el-row>

      <el-table v-loading="loading" :data="bannerList"
          @selection-change="handleSelectionChange"
          @row-dblclick="handleDetails"
      >
        <el-table-column type="selection" width="55" align="center"/>
        <el-table-column label="轮播Id" align="center" prop="bannerId" v-if="columns[0].visible"/>
        <el-table-column label="名称" align="center" prop="bannerName" v-if="columns[1].visible"/>
        <el-table-column label="轮播图" align="center" prop="bannerImg" v-if="columns[2].visible"/>
        <el-table-column label="跳转链接" align="center" prop="linkUrl" v-if="columns[3].visible"/>
        <el-table-column label="跳转类型" align="center" prop="type" v-if="columns[4].visible"/>
        <el-table-column label="排序" align="center" prop="sort" v-if="columns[5].visible"/>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width"
            v-if="columns[9].visible"
        >
          <template #default="scope">
            <el-button
                size="mini"
                type="text"
                icon="Edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['mall:banner:edit']"
            >修改
            </el-button>
            <el-button
                size="mini"
                type="text"
                icon="Document"
                @click="handleDetails(scope.row)"
                v-hasPermi="['mall:banner:query']"
            >详情
            </el-button>
            <el-button
                size="mini"
                type="text"
                icon="Delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['mall:banner:remove']"
            >删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
      />
      <div class="line-magin"></div>
    </div>
    <!-- 添加或修改轮播图对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="bannerForm" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="名称" prop="bannerName">
          <el-input v-model="form.bannerName" placeholder="请输入名称"/>
        </el-form-item>
        <el-form-item label="轮播图" prop="bannerImg">
          <el-input v-model="form.bannerImg" placeholder="请输入轮播图"/>
        </el-form-item>
        <el-form-item label="跳转链接" prop="linkUrl">
          <el-input v-model="form.linkUrl" placeholder="请输入跳转链接"/>
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input v-model="form.sort" placeholder="请输入排序"/>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <!-- 轮播图详情对话框 -->
    <el-dialog class="detail-form" :title="title" v-model="open2" width="700px" append-to-body>
      <el-form ref="bannerDetail" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="名称" prop="bannerName">
        {{ form.bannerName }}
      </el-form-item>
      <el-form-item label="轮播图" prop="bannerImg">
        {{ form.bannerImg }}
      </el-form-item>
      <el-form-item label="跳转链接" prop="linkUrl">
        {{ form.linkUrl }}
      </el-form-item>
      <el-form-item label="跳转类型" prop="type">
        {{ form.type }}
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        {{ form.sort }}
      </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Banner">
  import { listBanner, getBanner, delBanner, addBanner, updateBanner } from "@/api/mall/banner";
  const { proxy } = getCurrentInstance();

  // 遮罩层
  const loading = ref(true);
  // 导出遮罩层
  const exportLoading = ref(false);
  // 选中数组
  const ids = ref([]);
  // 非单个禁用
  const single = ref(true);
  // 非多个禁用
  const multiple = ref(true);
  // 切换搜索条件
  const showSearch = ref(false);
  // 总条数
  const total = ref(0);
  // 轮播图表格数据
  const bannerList = ref([]);
  // 弹出层标题
  const title = ref("");
  // 弹出层
  const open = ref(false);
  const open2 = ref(false);
  const data = reactive({
    // 列信息
    columns: [
      {key: 0, label: `轮播Id`, visible: true},
      {key: 1, label: `名称`, visible: true},
      {key: 2, label: `轮播图`, visible: true},
      {key: 3, label: `跳转链接`, visible: true},
      {key: 4, label: `跳转类型`, visible: true},
      {key: 5, label: `排序`, visible: true},
      {key: 6, label: `创建时间`, visible: true},
      {key: 7, label: `创建者`, visible: true},
      {key: 8, label: `更新时间`, visible: true},
      {key: 9, label: `操作`, visible: true},
    ],
    // 表单参数
    form:{},
    // 查询参数
    queryParams: {
      pageNum: 1,
      pageSize: 10,
      bannerName: null,
      bannerImg: null,
      linkUrl: null,
      type: null,
      sort: null,
    },
    rules: {
    }
  });
  const { columns, queryParams, form, rules } = toRefs(data);

  /** 查询轮播图列表 */
  function getList() {
    loading.value = true;
    listBanner(queryParams.value).then(response => {
      bannerList.value = response.rows;
      total.value = response.total;
      loading.value = false;
    });
  }
  /** 取消按钮 */
  function cancel() {
    open.value = false;
    open2.value = false;
    reset();
  }
  /** 表单重置 */
  function reset() {
    form.value = {
      bannerId: null,
      bannerName: null,
      bannerImg: null,
      linkUrl: null,
      type: null,
      sort: null,
      createTime: null,
      updateBy: null,
      updateTime: null,
    };
    proxy.resetForm("userForm");
  }
  /** 搜索按钮操作 */
  function handleQuery() {
    queryParams.value.pageNum = 1;
    getList();
  }

  /** 重置按钮操作 */
  function resetQuery() {
    proxy.resetForm("queryRef");
    handleQuery();
  }
  /** 切换按钮操作 */
  function changSearch() {
    showSearch.value = !showSearch.value;
  }
  /** 多选框选中数据 */
  function handleSelectionChange(selection) {
    ids.value = selection.map(item => item.bannerId);
    single.value = selection.length != 1;
    multiple.value = !selection.length;
  }
  /** 新增按钮操作 */
  function handleAdd() {
    reset();
    open.value = true;
    title.value = "添加轮播图";
  }
  /** 修改按钮操作 */
  function handleUpdate(row) {
    reset();
    const bannerId = row.bannerId || ids.value
    getBanner(bannerId).then(response => {
      form.value = response.data;
      open.value = true;
      title.value = "修改轮播图";
    });
  }
  /** 详情按钮操作 */
  function handleDetails(row) {
    reset();
    const bannerId = row.bannerId || ids.value
    getBanner(bannerId).then(response => {
      form.value = response.data;
      open2.value = true;
      title.value = "修改轮播图";
    });
  }
  /** 提交按钮操作 */
  function submitForm() {
    proxy.$refs["bannerForm"].validate(valid => {
      if (valid) {
        if (form.value.bannerId) {
          updateBanner(form.value).then(response => {
            proxy.$modal.msgSuccess("修改成功");
            open.value = false;
            getList();
          });
        } else {
          addBanner(form.value).then(response => {
            proxy.$modal.msgSuccess("新增成功");
            open.value = false;
            getList();
          });
        }
      }
    })
  }
  /** 删除按钮操作 */
  function handleDelete(row) {
    const bannerIds = row.bannerId || ids.value;
    proxy.$modal.confirm('是否确认删除轮播图编号为\"' + bannerIds + '\"的数据项?').then(() => {

      return delBanner(bannerIds);
    }).then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    }).catch(() => {});
  }
  /** 导出按钮操作 */
  function handleExport() {
    exportLoading.value = true;
    proxy.download("system/post/export", {
      ...queryParams.value
    }, `轮播图表_${new Date().getTime()}.xlsx`).then(response => {
      exportLoading.value = false;
    }).catch(error => {
      exportLoading.value = false;
    });
  }
  getList();
</script>
